﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Raider.WinForms.Actions.Properties;
using Raider.WinForms.Actions.Utils;

namespace Raider.WinForms.Actions.Standard
{
    /// <summary>
    /// Selects all text or items in the focused control.
    /// </summary>
    /// <remarks>
    /// List of supported client controls:
    /// <list type="bullet">
    /// <item><see cref="TextBoxBase"/></item>
    /// <item><see cref="ListBox"/></item>
    /// <item><see cref="ListView"/></item>
    /// </list>
    /// </remarks>
    [Description("Selects all items in the control.\nSupports all controls derived from TextBoxBase, ListBox and ListView.")]
    public class SelectAllAction : EditAction
    {
        public SelectAllAction()
            : base(Resources.SelectAll, Keys.Control | Keys.A, null)
        {
        }

        protected override void OnExecute(ExecuteEventArgs args)
        {
            base.OnExecute(args);

            var textBox = lastFocusedControl as TextBoxBase;
            if (textBox != null)
            {
                textBox.SelectAll();
                return;
            }

            var listBox = lastFocusedControl as ListBox;
            if (listBox != null)
            {
                try
                {
                    listBox.BeginUpdate();
                    for (int i = 0; i < listBox.Items.Count; i++)
                        listBox.SetSelected(i, true);
                }
                finally
                {
                    listBox.EndUpdate();
                }
                return;
            }

            var listView = lastFocusedControl as ListView;
            if (listView != null)
            {
                try
                {
                    listView.BeginUpdate();
                    foreach (ListViewItem item in listView.Items)
                        item.Selected = true;
                }
                finally
                {
                    listView.EndUpdate();
                }
                return;
            }
        }

        protected override void OnUpdate(UpdateEventArgs args)
        {
            base.OnUpdate(args);

            var textBox = lastFocusedControl as TextBoxBase;
            if (textBox != null)
            {
                Enabled = textBox.TextLength > 0 && textBox.SelectionLength < textBox.TextLength;
                return;
            }

            var listBox = lastFocusedControl as ListBox;
            if (listBox != null)
            {
                Enabled = (listBox.SelectionMode == SelectionMode.MultiExtended ||
                    listBox.SelectionMode == SelectionMode.MultiSimple) && 
                    listBox.SelectedItems.Count < listBox.Items.Count;
                return;
            }

            var listView = lastFocusedControl as ListView;
            if (listView != null)
            {
                Enabled = listView.MultiSelect = true && listView.SelectedItems.Count < listView.Items.Count;
                return;
            }
                
            Enabled = false;
        }
    }
}
